<template>
	<view class="content">
		<view class="btn-box">
			<button type="primary" size="mini" :disabled="scaning" @click="startScanBluetooth">开始扫描</button>
			<button size="mini" :disabled="!scaning" @click="stopScanBluetooth">停止扫描</button>
		</view>
		<scroll-view scroll-y enhanced enable-flex scroll-with-animation :show-scrollbar="false" class="list-scroll-box">
			<view v-for="item in list" :key="item.deviceId" class="item-box">
				<view class="left-box">
					<view style="font-size: 32rpx; color: #333">{{item.name}}</view>
					<view style="font-size: 20rpx">信号强度: {{item.RSSI}}dBm</view>
					<view style="font-size: 20rpx">UUID: {{item.deviceId}}</view>
				</view>
				<view class="right-box">
					<button type="primary" size="mini" @click="deviceConnection(item)">链接</button>
				</view>
			</view>
		</scroll-view>
		<!-- 底部填充-底部安全区域高度 -->
		<view :style="{height:safeAreaInsets.bottom+'px'}" />
	</view>
</template>

<script lang="ts" setup>
	import {ref} from 'vue';
	import {onLoad,onUnload} from '@dcloudio/uni-app';
	import {windowInfoStore} from '@/stores/windowInfo';
	
	const windowStore = windowInfoStore();
	const safeAreaInsets = windowStore.safeAreaInsets;
	
	const scaning = ref(false);
	const list = ref<UniApp.BluetoothDeviceInfo[]>([]);
	
	
	onLoad(()=>{
		uni.showLoading({title: '正在加载',mask:true});
		uni.openBluetoothAdapter().then(res=>{
			uni.hideLoading();
			console.log(res)
		}).catch(err=>{
			uni.hideLoading();
			console.log(err)
			uni.showToast({
				icon:'none',
				title:err.errMsg
			});
		});
		
		uni.onBluetoothAdapterStateChange((res)=>{
			if(!res.available){
				scaning.value = false;
				uni.showToast({
					icon:'none',
					title:'蓝牙适配器不可用'
				});
			}
		});
		
		uni.onBluetoothDeviceFound((res)=>{
			console.log(res);
			res.devices.forEach(item=>{
				const currDevice = list.value.find(device=>device.deviceId===item.deviceId);
				if(currDevice){
					currDevice.RSSI = item.RSSI;
				}else{
					list.value.push(item);
				}
			});
		});
	});
	
	onUnload(()=>{
		uni.offBluetoothDeviceFound();
		uni.offBluetoothAdapterStateChange();
	});
	
	//开始扫描蓝牙设备
	function startScanBluetooth(){
		uni.startBluetoothDevicesDiscovery({allowDuplicatesKey:true}).then(()=>{
			scaning.value = true;
			list.value = [];
		});
	}
	
	//停止扫描蓝牙设备
	function stopScanBluetooth(){
		uni.stopBluetoothDevicesDiscovery().then(()=>{
			scaning.value = false;
		});
	}
	
	//连接蓝牙设备
	function deviceConnection(item:UniApp.BluetoothDeviceInfo){
		uni.navigateTo({
			url:'/pages/bluetooth/bluteooth_service?deviceId='+item.deviceId,
		});
	}
	
	
</script>

<style lang="scss" scoped>
	.content {
		margin: 20rpx 30rpx;
		height: 100vh;
		display: flex;
		flex-direction: column;
		gap: 10rpx;
		.btn-box {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
		}
		.list-scroll-box {
			flex: 1;
			min-height: 0;
			border: 1px solid #f1f1f1;
			border-radius: 10rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			gap: 20rpx;
			padding: 30rpx;
			.item-box {
				display: flex;
				flex-direction: row;
				align-items: center;
				padding: 10rpx 0;
				border-bottom: 1px solid #f1f1f1;
			}
			.left-box {
				flex: 1;
				min-width: 0;
				display: flex;
				flex-direction: column;
				gap: 5rpx;
			}
			.right-box {
				
			}
		}
	}
</style>
